package com.chinamsp.codegen.util;

import java.io.StringWriter;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

/**
 * 模板工具类
 * @author shifenghu
 *
 */
public class TemplateUtils {

	/**
	 * engine
	 */
	private static VelocityEngine engine;
	
	
	/**
	 * 缓存
	 */
	//private static final Map<String, String> cacheds = new HashMap<String, String>(64);
	
	
	
	static {
		engine = new VelocityEngine();
		engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
		engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
		engine.init();
	}
	
	
	public static String render(String path, Map<String, Object> context) {
		StringWriter sw = new StringWriter();
		engine.getTemplate(path, "UTF8").merge(new VelocityContext(context), sw);
		return sw.toString();
	}
}
